package wcu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Database {
	
	private int numberOfProducts;
	
	public Database() {
		File fLog = new File("log.txt");
		File fStore = new File("store.txt");

		if (!fLog.exists()) {
			try {
				FileWriter fW = new FileWriter("log.txt");
				BufferedWriter bW = new BufferedWriter(fW);
				PrintWriter pW = new PrintWriter(bW);

				pW.write("");
				pW.close();

			} catch (IOException e) {
				e.printStackTrace();
			}

			if (!fStore.exists()) {
				try {
					FileWriter fW = new FileWriter("store.txt");
					BufferedWriter bW = new BufferedWriter(fW);
					PrintWriter pW = new PrintWriter(bW);

					String[] productList = setupStoreContent();
					this.numberOfProducts = productList.length;

					// Overwriting the txt file with the productList
					for (int i = 0; i < productList.length; i++) {
						pW.println(productList[i]);
					}
					pW.close();

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void updateStoreFile(int vareNummer, double mængdeILagerKg) {
		File fStore = new File("store.txt");

		if (fStore.exists() && !fStore.isDirectory()) {

			FileReader fR;
			try {
				fR = new FileReader("store.txt");
				BufferedReader bR = new BufferedReader(fR);

				Scanner sc = new Scanner(bR);
				ArrayList<String> newProductList = new ArrayList<String>();

				// This is badly optimized, but works
				while (sc.hasNextLine()) {
					String line = sc.nextLine();

					// Looks at one line and looks at the first part
					// "Varenummer=x" and checks x
					String[] lineSplit = line.split(",");
					if (lineSplit[0].contains(String.valueOf(vareNummer))) {
						line = lineSplit[0] + "," + lineSplit[1] + ","
								+ "Varenummer="
								+ String.valueOf(mængdeILagerKg);
					}
					// Creates the new list
					newProductList.add(line);
				}

				sc.close();

				// Overwrites the txt file with the new list
				FileWriter fW = new FileWriter("store.txt");
				BufferedWriter bW = new BufferedWriter(fW);
				PrintWriter pW = new PrintWriter(bW);

				for (int i = 0; i < newProductList.size(); i++) {
					pW.println(newProductList.get(i));
				}

				pW.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			System.out.println("NO FILE CALLED store.txt");
		}

		// if store file, write in specific way
		// Makes a new line for every product. 2 data, Varenummer, Varenavn,
		// mængde på lager i kg

		// Example:
		// Varenummer=2,Varenavn=Kage -
		// Varenummer=3,Varenavn=LagKage -

	}

	public String searchStoreFile(String vareNummer) {
		// Searches through the file for the product number line
		File fStore = new File("store.txt");

		if (fStore.exists() && !fStore.isDirectory()) {
			try {
				FileReader fR = new FileReader("store.txt");
				BufferedReader bR = new BufferedReader(fR);

				Scanner sc = new Scanner(bR);

				// This is badly optimized, but works
				while (sc.hasNextLine()) {
					String line = sc.nextLine();

					// Looks at one line and looks at the first part
					// "Varenummer=x" and checks x
					String[] vare = line.split(",");
					if (vare[0].contains(String.valueOf(vareNummer))) {
						return line;
					}
				}
				return "notfound";

			} catch (FileNotFoundException e) {
				// TODO: handle exception
			}
		}
		return "notfound";
	}

	public void writeToLogFile(int operatørNummer, int vareNummer, double afvejningIKg, double tilbagePåLager) {
		// Check if the file exists, if not create a new one
		FileWriter fW;
		try {
			//Time and date
			Date date = new Date();
			DateFormat dF1 = new SimpleDateFormat("dd/MM/yyyy");
			DateFormat dF2 = new SimpleDateFormat("HH:mm:ss");
			
			String calender = dF1.format(date);
			String time = dF2.format(date);
			
			fW = new FileWriter("log.txt", true);
			BufferedWriter bW = new BufferedWriter(fW);
			PrintWriter pW = new PrintWriter(bW);

			pW.println(calender + 
						"," + time +  
						"," +"OperatørNummer=" + operatørNummer + 
						"," + "VareNummer=" + vareNummer +
						"," + "AfvejningIKg=" + afvejningIKg + 
						"," + "TilbagePåLagerIKg=" + tilbagePåLager
						);
			
			pW.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		// dato, tid, operatørnummer, varenummer, afvejning (i kg)
		// og tilbage på lager (i kg) hver separeret med et komma. Makes a new
		// line for every 6 data

	}

	public int getNumberOfProducts(){
		return this.numberOfProducts;
	}
	
	private String[] setupStoreContent() {
		String[] list = { "Varenummer=1,Varenavn=Salt,MængdePåLagerKg=0.511",
				"Varenummer=2,Varenavn=Pepper,MængdePåLagerKg=0.4",
				"Varenummer=3,Varenavn=Sukker,MængdePåLagerKg=0.7",
				"Varenummer=4,Varenavn=Chokolade,MængdePåLagerKg=3.1",
				"Varenummer=5,Varenavn=Karry,MængdePåLagerKg=2.13",
				"Varenummer=6,Varenavn=Vanilla,MængdePåLagerKg=4.51",
				"Varenummer=7,Varenavn=Banan,MængdePåLagerKg=0.23",
				"Varenummer=8,Varenavn=Æble,MængdePåLagerKg=0.65",
				"Varenummer=9,Varenavn=Pære,MængdePåLagerKg=0.42",
				"Varenummer=10,Varenavn=Avocado,MængdePåLagerKg=0.2",
				"Varenummer=11,Varenavn=Appelsin,MængdePåLagerKg=0.132",
				"Varenummer=12,Varenavn=Citron,MængdePåLagerKg=0.111",
				"Varenummer=13,Varenavn=Ris,MængdePåLagerKg=1", };
		return list;
	}

}
